﻿

using Ma.Bussiness.AttributeExtend.AttributeModels;
using Ma.Bussiness.ModelExtend;
using System.Reflection;

namespace Ma.Bussiness.AttributeExtend
{
    public static class Cache
    {

        public static Dictionary<string, string> SqlCache = new Dictionary<string, string>();
        public static Dictionary<PropertyInfo, string> ColumnChineseNameCache = new Dictionary<PropertyInfo, string>();
        public static Dictionary<PropertyInfo, string> ColumnMappingNameCache = new Dictionary<PropertyInfo, string>();
        public static string GetSelectSql<T>() where T : BaseModel
        {
            Type type = typeof(T);

            string sqlKey = $"SELECT_{AttributeHelper.GetMappingTName<T>()}";
            if (Cache.SqlCache.ContainsKey(sqlKey))
            {
                return SqlCache[sqlKey];
            }
            else
            {
                string columnName = string.Join(",", type.GetProperties().Select(prop => $"[{prop.GetMappingColumnName()}]"));
                var sql = $"SELECT {columnName} FROM [{AttributeHelper.GetMappingTName<T>()}]";
                SqlCache.Add(sqlKey, sql);
                return sql;
            }
        }

        /// <summary>
        /// 缓存字段上中文名称
        /// </summary>
        /// <param name="property"></param>
        /// <returns></returns>
        public static string GetColumnChineseName(PropertyInfo property)
        {
            if (!ColumnChineseNameCache.ContainsKey(property))
            {
                string columnName = string.Empty;
                if (property.IsDefined(typeof(ColumnAttribute), true))
                {
                    ColumnAttribute attribute = (ColumnAttribute)property.GetCustomAttribute(typeof(ColumnAttribute), true);
                    columnName = attribute.ColumnName;
                }
                else
                    columnName = property.Name;
                ColumnChineseNameCache.Add(property, columnName);
                return columnName;
            }
            else
            {
                return ColumnChineseNameCache[property];
            }
        }

        /// <summary>
        /// 缓存实体属性对应的数据库中的真实字段
        /// </summary>
        /// <param name="property"></param>
        /// <returns></returns>
        public static string GetColumnMappingName(PropertyInfo property)
        {
            if (!ColumnMappingNameCache.ContainsKey(property))
            {
                string mappingName = string.Empty;
                if (property.IsDefined(typeof(MappingAttribute), true))
                {
                    MappingAttribute attribute = (MappingAttribute)property.GetCustomAttribute(typeof(MappingAttribute), true);
                    mappingName = attribute.MappingName;
                }
                else
                    mappingName = property.Name;

                ColumnMappingNameCache.Add(property, mappingName);
                return mappingName;
            }
            else
            {
                return ColumnMappingNameCache[property];
            }
        }

    }
    public static class CacheHelper<T>
    {
        public static string MappingName = null;
        static CacheHelper()
        {
            {
                Type type = typeof(T);
                if (type.IsDefined(typeof(MappingAttribute), true))
                {
                    MappingAttribute attribute = (MappingAttribute)type.GetCustomAttribute(typeof(MappingAttribute), true);
                    MappingName = attribute.MappingName;
                }
                else
                    MappingName = type.Name;
            }
        }

    }

    /// <summary>
    /// 缓存实体属性上的特性集合
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public static class CachePropertyAttribute<T> where T : BaseModel
    {
        public static Dictionary<PropertyInfo, object[]> Property_Attribute_Cache = new Dictionary<PropertyInfo, object[]>();
        static CachePropertyAttribute()
        {
            Type type = typeof(T);
            foreach (PropertyInfo prop in type.GetProperties())
            {
                Property_Attribute_Cache.Add(prop, prop.GetCustomAttributes(typeof(AbstractValidateAttribute), true));
            }
        }
    }


}
